new Handle:g_SpawnPlayer = INVALID_HANDLE
new Handle:g_SpawnDelay = INVALID_HANDLE
new Handle:g_OldRespawnSystem = INVALID_HANDLE
new Handle:g_VoteExtend = INVALID_HANDLE
new Handle:m_Spawn
new Handle:m_PreSpawn

public Action:OnPlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"))
	if (client <= 0){
		return Plugin_Handled
	}
	if(GetConVarInt(g_SpawnPlayer) == 1 && !IsSpec[client]){
		SpawnPlayer(client)
	}
	if (GetConVarInt(g_DissolvePlayer) == 1){
		DissolvePlayer(client)
	}
	if(GetConVarInt(g_TelePlayer) == 1){
		CheckTeleport(client)
	}
	if (GetConVarInt(g_Save) == 1 && GetConVarInt(g_SaveDelay) > 0 ){
		if (m_timer == INVALID_HANDLE){
			if (Debug)
				PrintToChat(client, "timer invalide?!")
			return Plugin_Handled
		}
		else if (m_timer != INVALID_HANDLE){
			DO_KillTimer()
		}
	}
	return Plugin_Handled
}

public Action:SpawnPlayer(client)
{
	if (client <= 0){
		return Plugin_Handled
	}

	new clients = GetClientCount(true)
	if	(clients <= 1)
	{
		if (Debug)
			PrintToChat(client, "CLIENT <= 1 NOT RESPAWNING")
		return Plugin_Handled
	}

	if (IsClientConnected(client) && IsClientInGame(client) && (Synergy))
	{
		new Float:m_Delay3 = GetConVarFloat(g_SpawnDelay)
		if (m_Delay3>=2.5)
		{
			if (Debug)
				PrintToChat(client, "MAINREPEAT")

			CreateTimer((m_Delay3 - 0.5), DO_PreSpawn, client)
			CreateTimer(m_Delay3, DO_SpawnPlayer, client)
		}
		else
		{
			PrintToServer("[SYN] ERROR: Spawn delay must above a value of 2.5!")
			return Plugin_Handled
		}
	}
	else
	{
		return Plugin_Handled
	}
	return Plugin_Handled
}

public Action:DO_PreSpawn(Handle:timer, any:client)
{
	if (IsClientConnected(client) && IsClientInGame(client) && (Synergy)){
		if (Debug)
			PrintToChat(client, "PRESPAWN")
		
		SDKCall(m_PreSpawn, client)
		return Plugin_Handled
	}
	return Plugin_Handled
}

public Action:DO_SpawnPlayer(Handle:timer, any:client)
{
	if (IsClientConnected(client) && IsClientInGame(client) && (Synergy)){
		if (Debug)
			PrintToChat(client, "SPAWN")
		
		SDKCall(m_Spawn, client)
		IsSpec[client] = false
		IsRespawn[client] = false
		return Plugin_Handled
	}
	return Plugin_Handled
}

public g_SpawnPlayerChanged(Handle:convar, const String:oldValue[], const String:newValue[])
{
	if (GetConVarInt(g_SpawnPlayer) == 1)
	{
		SetConVarInt(FindConVar("mp_respawndelay"), 0)
	}
	else
	{
		SetConVarInt(FindConVar("mp_respawndelay"), 1)
	}
}

public OnMapStart()
{
	PrecacheSound(SOUND_UP, true)
	PrecacheSound(SOUND_TELE, true)
	PrecacheModel(SPRITETRAIL, true)
	g_LightningSprite = PrecacheModel("materials/sprites/lgtning.vmt")
	
	CheckMapEnd = false

	if(GetConVarInt(g_Custom) == 1)
		Custom_Files()

	if (GetConVarInt(g_VoteExtend) == 1)
		CreateTimer(5.0, CheckLeft)

	DO_VehicleFix()
}